package com.atguigu.gulimall.order.service.impl;

import com.atguigu.gulimall.order.entity.member.TestUser;
import com.atguigu.gulimall.order.entity.ware.TestWare;
import com.atguigu.gulimall.order.feign.MemberFeign;
import com.atguigu.gulimall.order.feign.WareFeign;
import com.atguigu.gulimall.order.service.TestOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.atguigu.gulimall.order.dao.TestOrderMapper;
import com.atguigu.gulimall.order.entity.TestOrder;

@Service
public class TestOrderServiceImpl implements TestOrderService {

    @Resource
    private TestOrderMapper testOrderMapper;

    @Resource
    private WareFeign wareFeign;

    @Resource
    private MemberFeign memberFeign;

    @GlobalTransactional(name = "scorpios-create-order",rollbackFor = Exception.class)
    @Override
    public int insert(TestOrder record) {
        int update = 0;
        try {
            update = testOrderMapper.insert(record);
            memberFeign.amountService(TestUser.builder()
                    .id(Integer.valueOf(record.getUserId()))
                    .use(record.getTotal()).build());

            wareFeign.updateQty(TestWare.builder()
                    .id(Integer.valueOf(record.getProductId()))
                    .use(record.getCount()).build());
            testOrderMapper.update(record.getId(),1);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return update;
    }


}
